/*
 * Created on 2007-4-16
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;

import dao.WebUserDAO;
import entity.WebUser;



/**
 * @author 
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
@WebServlet("/usercheak")
public class UserServlet extends HttpServlet {
    private WebUserDAO dao = new WebUserDAO();
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        //TODO Method stub generated by Lomboz
        this.doPost(request, response);
    }
    
    private String logincheck(HttpServletRequest request) {
        try {
            String userno=request.getParameter("userno");
            if(userno==null ||userno.equals(""))
                throw new Exception("请输入用户编号");
            String pwd=request.getParameter("password");
            WebUser user=dao.readUser(userno);
            if(user==null)
                throw new Exception("用户不存在");
            if(!pwd.equals(user.getPwd()))
               throw new Exception("密码错误");
            //request.getSession().setAttribute("userno",user.getUserno());
            //if("admin".equals(user.getUsertype()))
            //   return "/home_page.jsp";
            //else
            //    return "/home_page.jsp";
            String usertype=dao.getType(userno);
            if(userno.equals(user.getUserno())&&pwd.equals(user.getPwd()))
            {
            	if(usertype.equals("1"))
            	    {return "/home_page1.jsp";}
            	else
            		{return "/home_page2.jsp";}
            }
            else
            	return "/login.jsp";
            
        } catch (Exception ex) {
            request.getSession().setAttribute("errormsg", ex.getMessage());
            return "/login.jsp";
        }
    }

    private String list(HttpServletRequest request) {
        try {
            request.setAttribute("objlist", dao.loadAllUsers());
            return "/login.jsp";
        } catch (Exception ex) {
            request.setAttribute("errormsg", ex.getMessage());
            return "/error.jsp";
        }
    }

    private String adduser(HttpServletRequest request) {
    	
        request.setAttribute("obj", new WebUser());
        return "/user_edit.jsp";
    }

    private String adduserresult(HttpServletRequest request) {
    	
        WebUser user = new WebUser();
        user.setDepartment(request.getParameter("department"));
        user.setPwd(request.getParameter("password"));
        user.setUsertype(request.getParameter("usertype"));
        user.setUserno(request.getParameter("userno"));
        try {
        	 String userno=request.getParameter("userno");
             if(userno==null ||userno.equals(""))
                 throw new Exception("用户编号不能为空");
             String pwd=request.getParameter("password");
             if(pwd==null ||pwd.equals(""))
                 throw new Exception("密码不能为空");
            this.dao.addUser(user);
            return this.list(request);
        } catch (Exception ex) {
        	  request.getSession().setAttribute("errormsg", ex.getMessage());
              return "/user_add.jsp";
        }

    }

    private String modifyuser(HttpServletRequest request) {
        try {
            WebUser user = this.dao.readUser(request.getParameter("userno"));
            user.setMethod("modifyuserresult");
            request.setAttribute("obj", user);
            return "/user_edit.jsp";
        } catch (Exception ex) {
            request.setAttribute("errormsg", ex.getMessage());
            return "/error.jsp";
        }
    }

    private String modifyuserresult(HttpServletRequest request) {
        WebUser user = new WebUser();
        user.setDepartment(request.getParameter("department"));
        user.setPwd(request.getParameter("password"));
        user.setUsertype(request.getParameter("usertype"));
        user.setUserno(request.getParameter("userno"));
        try {
            this.dao.saveUser(user);
            return this.list(request);
        } catch (Exception ex) {
            request.setAttribute("errormsg", ex.getMessage());
            return "/error.jsp";
        }
    }

    private String deleteuser(HttpServletRequest request) {
        try {
            this.dao.delUser(request.getParameter("userno"));
            return this.list(request);
        } catch (Exception ex) {
            request.setAttribute("errormsg", ex.getMessage());
            return "/error.jsp";
        }
    }

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String method = request.getParameter("method");
        if (method == null)  method = "logincheck";
        String result = "";
        if (method.equals("adduser"))
            result = adduser(request);
        else if (method.equals("adduserresult"))
            result = adduserresult(request);
        else if (method.equals("modifyuser"))
            result = modifyuser(request);
        else if (method.equals("modifyuserresult"))
            result = modifyuserresult(request);
        else if (method.equals("deleteuser"))
            result = deleteuser(request);
        else if (method.equals("logincheck"))
            result = this.logincheck(request);
        else
            result = list(request);
        RequestDispatcher dispatcher = request.getSession().getServletContext()
                .getRequestDispatcher(result);
        if (dispatcher != null)
            dispatcher.forward(request, response);
    }

    /*
     * (non-Javadoc)
     * 
     * @see javax.servlet.Servlet#destroy()
     */
    public void destroy() {
        // TODO Auto-generated method stub
        dao.release();
        super.destroy();
    }

}
